import Vue from 'vue'
import Vuex from 'vuex'
import app from './modules/app.js'
import _ from 'lodash'
import * as util from '../common/util'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    /*
    * 运行到浏览器  NODE_ENV: "development" VUE_APP_PLATFORM: "h5"
    * 运行到小程序  NODE_ENV: "development" VUE_APP_PLATFORM: "mp-weixin"
    * 发行到小程序  NODE_ENV: "production" VUE_APP_PLATFORM: "mp-weixin"
    * */
    env: {// 环境
      env: process.env.NODE_ENV,
      platform: process.env.VUE_APP_PLATFORM,
      dev: process.env.NODE_ENV === 'development',
      h5: process.env.VUE_APP_PLATFORM === 'h5',
    },
    userInfo: {// 用户信息
      id: undefined, // 需要设置默认值,以供vuex响应
      // activitiSync: 1
      // address: null
      // avatar: null
      // bank: null
      // bankAccount: null
      // birthday: null
      // clientId: null
      // companyId: "3d464b4ea0d2491aab8a7bde74c57e95"
      // createBy: "admin"
      // createTime: "2022-06-17 06:48:41"
      // delFlag: 0
      // departIds: ""
      // domicile: null
      // email: null
      // hobby: null
      // id: "1537567868770123777"
      // idNumber: null
      // nature: null
      // orgCode: "A03A01A01"
      // orgCodeTxt: null
      // phone: "15012705251"
      // politicalStatus: null
      // post: null
      // realname: "李比"
      // relTenantIds: null
      // sex: null
      // status: 1
      // telephone: null
      // updateBy: "admin"
      // updateTime: "2022-06-17 06:50:54"
      // userIdentity: 1
      // userType: 1
      // username: "business01"
      // workNo: "B002"
    },
    sysAllDictItems:[],
    departs:[],
    customer:{},



    wxInfo: {},
    appInfo: {// 应用信息
      // admin_id: 1,
      // app_id: "123456",
      // app_key: "1a26f5cbdfb7ed1768c470055bbff4fb",
      // is_login: 1,
      // store_name: "欧健康",
      // is_default
    },
    userLocation: { // 用户当前位置信息
      // latitude
      // longitude
      // city
      // province
    },
    diyConfig: { //
    },

    // 获取分享数据 各个页面分享
    share: {
      title: '',
      image_url: util.$bgPath + '/icon_invite_out_01.png',
    },

    loginPromise: null,
    loginModalVisible: false,

    // 首页相关
    home: {
      modalLoginVisible: false,
    },
    // 选中的城市
    cityChoose: {
      'province': '',
      'city': '',
      'province_code': 0,
      'city_code': 0,
      'latitude': 0,
      'longitude': 0,
    },
    // 订单支付页面相关
    orderPay: {
      payType: -1, // 1微信支付 2余额支付 3：期限卡支付
      coupon: { // 优惠券
        id: '',
        type: '',
        price: '',
        discount: '',
      },
      card: { // 期限卡
        id: '',
        name: '',
        type: '',
      },
    },

    // 订单状态列表
    orderStatusMap: [
      { label: '全部', value: 5, color: '' },
      { label: '未支付', value: 0, color: '#F35D51' },
      { label: '未使用', value: 1, color: '#FFAA16' },
      { label: '进行中', value: 2, color: '#3FB271' },
      { label: '提前结束', value: 3, color: '#6E7C89' },
      { label: '已完成', value: 4, color: '#6E7C89' },
    ],

    // 设备
    device: {
      info1: {},
    },

  },

  mutations: {
    modelSet: (state, payload) => {
      _.set(state, payload.path, payload.data)
    },
    modelAssign: (state, payload) => {
      if (_.isArray(payload.data)) {// 数组
        _.each(payload.data, (value, key) => {
          _.set(state, `${payload.path}[${key}]`, value)
        })
      } else if (_.isPlainObject(payload.data)) {// 普通对象
        let obj = _.get(state, payload.path)
        _.each(payload.data, (value, key) => {
          if (obj.hasOwnProperty(key)) {
            _.set(state, `${payload.path}.${key}`, value)
          } else {
            Vue.set(obj, key, value)
          }
        })
      } else {// string num boolean ...
        _.set(state, payload.path, payload.data)
      }
    },
  },

  actions: {
    async modelSetWithServer({ commit, state }, payload){
      try {
        let response = {}
        switch (payload.path) {
          case 'list':
            response = await Promise.resolve([1, 2, 3])
            break
        }
        commit('modelSet', { path: payload.path, data: response.data })
        return response
      } catch (e) {
        return Promise.reject(e)
      }
    },
  },
  modules: {
    app,
  },
})
